<?php

namespace App\Events;

use App\Model\Business\Schedule;
use App\Model\Business\ScheduleStuden;
use App\Model\Eeoapi\Eeoschedule;
use App\Model\Foundation\Teacher;
use App\Model\System\Eeofile;

class ScheduleEvent extends Event
{
    # eeo 课程 id
    public  $courseId;
    # eeo 课节 id
    public  $classId;
    # eeo 课节 name
    public  $className;
    # eeo teacher account
    public  $teacherAccount;
    # eeo teacher name
    public  $teacherName;
    # eeo teacher photo
    public  $Filedata;
    # 目录ID
    public  $folderId;
    # chimp 课程开始时间
    public  $beginTime;
    # chimp 课程结束时间
    public  $endTime;
    # 台上人数
    public $seatNum;

    # 直播参数
    public $record;
    public $live;
    public $replay;

    # 回放直播地址
    public $webcast;

    # 学生
    private $students;
    private $oldStudents;

    # schedule
    public $scheduleid;


    /**
     * LessonEvent constructor.
     * @param Schedule $schedule
     */
    public function __construct($schedule, $action)
    {
        parent::__construct();
        $this->students = [];
        $this->oldStudents = [];
        $this->Filedata = '';
        $this->scheduleid = $schedule->scheduleid;

        $this->seatNum = $schedule->classtypeid;
        $this->action = $action;
        $this->className = $schedule->name;
        $this->endTime = strtotime($schedule->endtime);
        $this->folderId = $schedule->folderid;
        $this->beginTime = strtotime($schedule->begintime);
        $this->record = $schedule->record;
        $this->live = $schedule->live;
        $this->replay = $schedule->replay;

        # 获取教师信息
        $teacher = Teacher::find($schedule->teacheruid);
        $this->teacherAccount = $teacher->ClassInNo;
        $this->teacherName = $teacher->lastname;
	    $photo = $teacher->photo;
        if(!empty($photo)){
            $photo = str_replace('/api/file/', '', $photo);
            $photo = str_replace('/api/file', '', $photo);
            $photo = str_replace('?u=', '', $photo);
            $photo = Eeofile::getLocalPath($photo);
            if(file_exists($photo))
                $this->Filedata = $photo;
        }
        # 获取映射id
        if($action<>Event::CREATE){
            $obj = Eeoschedule::where('scheduleid', $schedule->scheduleid)->first();
	    if($obj){
                $this->courseId = $obj->courseId;
                $this->classId = $obj->classId;
            }
        }
    }

    /**
     * 设置课节id
     * @param $id
     */
    public function setClassID($id)
    {
        $this->classId = $id;
        $fileds = [
            'classId'=>$this->classId,
            'courseId'=>$this->courseId,
            'scheduleid'=>$this->scheduleid
        ];
        Eeoschedule::firstOrCreate($fileds);
    }

    /**
     * 设置课程ID
     * @param $id
     */
    public function setCourseID($id)
    {
        $this->courseId = $id;
    }

    /**
     * 删除课节ID
     */
    public function delClassID()
    {
        Eeoschedule::where('classId',$this->classId)->delete();
    }

    /**
     * 删除课节
     */
    public function delSchedule()
    {
        # 清除学生关系
        ScheduleStuden::where('scheduleid', $this->scheduleid)->delete();
        # 删除课节
        Schedule::destroy($this->scheduleid);
    }

    /**
     * 设置返回的课节直播/回放地址
     * @param $data
     */
    public function setWebcast($data)
    {
        if(!empty($data)) {
            $schedule = Schedule::find($this->scheduleid);
            $schedule->webcast = $data;
            $schedule->save();
        }
    }
    /**
     * 返回学生信息
     * @return array
     */
    public function getStudents($old=false)
    {
        if($old){
            return $this->oldStudents;
        }
        return $this->students;
    }

    /**
     * 附加学生
     * @param $student
     */
    public function setStudent($student, $old=false)
    {
        if($old){
            $id = $student->studentuid;
            if(!key_exists($id, $this->students)){
                $this->oldStudents[$id]['identity'] = 1;
                $this->oldStudents[$id]['studentName'] = $student->name;
                $this->oldStudents[$id]['studentAccount'] = $student->ClassInNo;
            }
        }else{
            $id = $student->studentuid;
            if(!key_exists($id, $this->students)){
                $this->students[$id]['identity'] = 1;
                $this->students[$id]['studentName'] = $student->name;
                $this->students[$id]['studentAccount'] = $student->ClassInNo;
            }
        }
    }

    /**
     * 批量添加学生
     * @param Student $students
     */
    public function setStudents($students, $old=false)
    {
        foreach($students as $student){
            $this->setStudent($student, $old);
        }
    }

    /**
     *
     */
    function __destruct()
    {
    }

}
